#ifndef _SESSIONHANDLER_H
#define _SESSIONHANDLER_H

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#pragma once
#include "concurrent\Runnable.h"
#include "MultiplayerGameServer.h"
#include "ByteBuffer.h"
#include "GameEvent.h"
#include <iostream>
#include <list>
#include <fstream>
#include <winsock2.h>

using namespace concurrent;
using namespace std;

class MultiplayerGameServer;

/*
 * Handles a game session started by a player.
 * It is responsible for reading from the network connection, creating the commands
 * or events out of the information received, and processing those commands and
 * events, giving a response to the client.
 */
class SessionHandler : public Runnable
{
public:
	SessionHandler(int, SOCKET, MultiplayerGameServer*);
	virtual ~SessionHandler(void);
	int getId(void);
	SOCKET getSocket(void);
	void update(GameEvent*);
	bool isInvalidated(void);
	
protected:
	virtual DWORD run(void);
private:
	int handleSession(void);
	void processEvent(GameEvent*);
	void sendSessionId(void);
	void sendBackground(string);
	void sendSceneState(void);
	int id;
	SOCKET socket;
	MultiplayerGameServer* server;
	bool _isInvalidated;
	bool _isInitialized;
};

#endif
